package com.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.Department;
import com.services.DepartmentService;

/**
 * Servlet implementation class DepartmentServlet
 */
@WebServlet("/department")
public class DepartmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DepartmentService dep = new DepartmentService();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DepartmentServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		if (method == null) {
			method = "selectAll";
		}
		switch (method) {
		case "selectAll":
			List<Department> list = dep.selectAll();
			request.setAttribute("list", list);
			request.getRequestDispatcher("department.jsp").forward(request, response);
			break;
		case "delete":
			String departmentStr = request.getParameter("id");
			int departmentSno = Integer.parseInt(departmentStr);
			dep.delete(departmentSno);
			response.sendRedirect("/salary_manage/department");
			break;
		case "findById":
			departmentStr = request.getParameter("id");
			departmentSno = Integer.parseInt(departmentStr);
			request.setAttribute("department", dep.findById(departmentSno));
			request.getRequestDispatcher("updateDepartment.jsp").forward(request, response);
			break;
		case "add":
			request.getRequestDispatcher("addDepartment.jsp").forward(request, response);
			break;

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		switch (method) {
		case "add":
			String departmentStr = request.getParameter("departmentSno");
			int departmentSno = Integer.parseInt(departmentStr);
			String departmentName = request.getParameter("departmentName");
			String amountStr = request.getParameter("amount");
			int amount = Integer.parseInt(amountStr);
			String phoneNum = request.getParameter("phoneNum");
		
			dep.add(departmentSno, departmentName, amount, phoneNum);
			break;
		case "update":
			departmentStr = request.getParameter("departmentSno");
			departmentSno = Integer.parseInt(departmentStr);
			departmentName = request.getParameter("departmentName");
			amountStr = request.getParameter("amount");
			amount = Integer.parseInt(amountStr);
			phoneNum = request.getParameter("phoneNum");
			
			dep.update(departmentSno, departmentName, amount, phoneNum);
			break;
		}
		response.sendRedirect("/salary_manage/department");

	}

}
